package com.example.yunhailutong.zhongtan.activity;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.yunhailutong.zhongtan.R;
import com.example.yunhailutong.zhongtan.app.App;
import com.example.yunhailutong.zhongtan.app.OldsEvent;
import com.example.yunhailutong.zhongtan.entity.HangYeZiXunEntity;
import com.example.yunhailutong.zhongtan.net.PersionBiz;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Red_Fox on 2017/11/30.
 */

public class ActivityHangYeZiXun extends ActivityBase {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.WebView)
    android.webkit.WebView webView;
    PersionBiz persionBiz;



    @Override
    protected void addListener() {

    }

    @Override
    protected void initActivity(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        persionBiz = new PersionBiz();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        EventBus.getDefault().register(this);
        Map<String, String> params = new HashMap<>();
        params.put("id", getIntent().getStringExtra("id"));
        title.setText("行业资讯");
        persionBiz.GetHangYeXiangQing(params);
        showWaitingDialog();
    }

    @Override
    protected int contentViewById() {
        return R.layout.activity_hangyezixun;
    }

    /**
     * 课程表
     *
     * @param msg
     */
    @Subscribe
    public void notice(OldsEvent.HangYeZiXunDescEvent msg) {
        dismissWaitingDialog();
        if (msg.what == 1) {
            HangYeZiXunEntity entity = (HangYeZiXunEntity) msg.obj;
            try {
                final String html =  java.net.URLDecoder.decode(entity.getData().getContent(),   "utf-8");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        loadHtmlData(html);
                    }
                });

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                disPlay("解码失败请稍后重试");
            }

        } else {
            disPlay("网络加载失败");
        }
    }

    public void loadHtmlData(String data){
        webView.setWebViewClient(new WebViewClient());
        String html=getHtmlData(data);
        //使用简单的loadData()方法总会导致乱码，有可能是Android API的Bug
        //webView.loadData(data, "text/html", "GBK");
        webView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
    }
    private String getHtmlData(String bodyHTML) {
        String head = "<head>"
                + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> "
                + "<style>img{max-width: 100%; width:auto; height:auto;}</style>"
                + "</head>";
        return "<html>" + head + "<body>" + bodyHTML + "</body></html>";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation

    }
}
